package org.jetlinks.community.mstn.entity.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.hswebframework.ezorm.core.utils.StringUtils;
import org.jetlinks.community.auth.entity.UserDetail;
import org.jetlinks.community.auth.enums.RoleType;
import org.jetlinks.community.auth.service.request.SaveUserRequest;

import java.util.Collections;
import java.util.HashSet;

@Data
public class UserCreateReqVO {

    @Schema(description = "用户ID")
    private String userId;

    @Schema(description = "租户套餐ID")
    private String roleId;

    @Schema(description = "用户昵称")
    private String name;

    @Schema(description = "用户账号")
    private String username;

    @Schema(description = "密码")
    private String password;

    @Schema(description = "联系人手机号,可为空")
    private String phone;

    @Schema(description = "描述")
    private String description;



    public UserDetail getUserDetail(String orgId){
        UserDetail detail = new UserDetail();
        detail.setId(getUserId());
        detail.setUsername(getUsername());
        detail.setPassword(getPassword());
        if(StringUtils.isNullOrEmpty(getName())){
            //不修改
            if(StringUtils.isNullOrEmpty(getUsername())){
            } else {
                detail.setName(getUsername());
            }
        } else {
            detail.setName(getName());
        }
        detail.setTelephone(getPhone());
        detail.setTenantId(orgId);
        detail.setType(RoleType.tenantUser.getValue());
        detail.setDescription(getDescription());
        return detail;
    }

    public SaveUserRequest getSaveUserRequest(String orgId){
        SaveUserRequest request = new SaveUserRequest();
        request.setUser(getUserDetail(orgId));
        request.setRoleIdList(new HashSet<>(Collections.singletonList(getRoleId())));
        request.setOrgIdList(new HashSet<>(Collections.singletonList(orgId)));
        return request;
    }


    //通过UserDetail转换
    public static UserCreateReqVO of(UserDetail detail){
        UserCreateReqVO vo = new UserCreateReqVO();
        vo.setUserId(detail.getId());
        vo.setUsername(detail.getUsername());
        vo.setDescription(detail.getDescription());
        vo.setName(detail.getName());
        vo.setPhone(detail.getTelephone());
        vo.setRoleId(detail.getRoleList().get(0).getId());
        return vo;
    }
}
